<?php

namespace common\models\notice;

use common\helpers\Url;
use common\logics\ShareLogic;
use common\logics\UserLogic;
use common\models\user\User;
use common\models\actor\Actor;
use common\models\user\UserAccount;
use Yii;

/**
 * This is the model class for table "{{%notice}}".
 *
 * @property int $id
 * @property int $user_id 推送人ID
 * @property string $title 标题
 * @property string $description 消息描述
 * @property string $location 地点
 * @property int $salary 薪酬/薪资要求：0为无薪酬，留空为不显示，非0为薪酬数值
 * @property int $experience 经验年限：0为不限
 * @property int $starttime 拍摄开始时间
 * @property int $endtime 拍摄结束时间
 * @property int $type_id 通告类型ID
 * @property int $weight 权重
 * @property int $hits 访问次数
 * @property int $duration 展示周期（秒）:0为一直有效。
 * @property int $status 状态：0未发布，1已提交发布，待审核，10已审核发布中
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 * @property UserLogic $user
 * @property Actor $actor
 */
class Notice extends \common\db\ActiveRecord
{

    use \common\traits\Constants;
    use \common\traits\relation\UserData;

    const SALARY_ON = 1;
    const SALARY_OFF = 0;

    const EXPERIENCE_ON = 1;
    const EXPERIENCE_OFF = 0;
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    public $roleNames;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%notice}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'salary', 'experience', 'starttime', 'endtime', 'type_id', 'weight', 'hits', 'duration', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['title', 'description', 'starttime', 'endtime', 'location', 'salary', 'experience'], 'required'],
            [['title'], 'string', 'max' => 100],
            [['description'], 'string', 'max' => 255],
            [['location'], 'string', 'max' => 50],
            ['hits', 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '推送人ID',
            'title' => '标题',
            'description' => '消息描述',
            'location' => '地点',
            'salary' => '薪酬/薪资要求：0为无薪酬，留空为不显示，非0为薪酬数值',
            'experience' => '经验年限：0为不限',
            'starttime' => '拍摄开始时间',
            'endtime' => '拍摄结束时间',
            'type_id' => '通告类型ID',
            'weight' => '权重',
            'hits' => '访问次数',
            'duration' => '展示周期（秒）:0为一直有效。',
            'status' => '状态：0未发布，1已提交发布，待审核，10已审核发布中',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getUsername()
    {

        return $this->user ? $this->user->username : '';
    }

    public function getActor()
    {
        return $this->hasOne(Actor::class, ['user_id' => 'user_id']);
    }

    public function getActorId()
    {
        return $this->actor ? $this->actor->id : '';
    }

    public function getNeedRoles()
    {
        return $this->hasMany(NoticeNeed::class, ['notice_id' => 'id']);
    }

    public function getRoles()
    {
        return $this->hasMany(ShowbizRole::class, ['id' => 'role_id'])->via('needRoles');
    }

    public function getAttach()
    {
        return $this->hasMany(NoticeAttach::class, ['notice_id' => 'id']);
    }

    public function getSendActor()
    {
        return $this->hasOne(NoticeReceive::class, ['notice_id' => 'id']);
    }

    public function getSendTag()
    {
        return $this->getSendActor()->andWhere(['from_user_id' => Yii::$app->user->id]);
    }

    public function getIssend()
    {
        return $this->sendTag ? true : false;
    }

    public function getAccount()
    {
        return $this->hasOne(UserAccount::class, ['user_id' => 'user_id']);
    }

    public function getAmount()
    {
        return $this->account ? $this->account->amount : 0;
    }

    public function getIcon()
    {
        return $this->amount ? Url::attach('assets/contribute/contribute_icon.png') : '';
    }
//    /**
//     * 获取商品分享数据
//     *
//     * @return array
//     */
//    public function getShareData() {
//        $model = new ShareLogic();
//        $model->setAttributes([
//            'type' => ShareLogic::TYPE_NOTICE,
//            'title' => $this->title,
//            'content' => $this->description,
//            'thumb' => $this->user->getPortrait(),
//        ]);
//        $data = [
//            'id' => $this->id,
//        ];
//        $user = Yii::$app->user->identity;
//        if ($user !== null) {
//            $data['from_user_id'] = $user->id;
//        }
//        $model->setParams($data);
//        return $model->toArray();
//    }

    //    public function getNoticeReceive()
    //    {
    //        return $this->hasMany(NoticeReceive::class, ['notice_id' => 'id']);
    //    }
    //    public function getNoticeResume()
    //    {
    //        return $this->hasMany(NoticeResume::class, ['notice_id' => 'id']);
    //    }
    //
    //    public function getResume()
    //    {
    //        return $this->hasMany(Actor::class, ['id' => 'actor_id'])->via('noticeResume');
    //    }
    public function fields()
    {
        return [
            'id',
            //            'user_id',
            'user' => 'userText',
            //            'resume',
            'title',
            'description',
            'location',
            'salary',
            'experience',
            'starttime',
            'endtime',
            'attach',
            'hits',
            'status',
            'created_at',
        ];
    }

}
